package mp

import (
	"duoke/model"
	"duoke/service"
	"duoke/utils/gconv"
	"duoke/utils/request"
	"duoke/utils/response"
	"github.com/gin-gonic/gin"
)

var CourseList = new(courseListCtl)

type courseListCtl struct{}

func (ctl *courseListCtl) List(c *gin.Context) {
	req := request.JsonMap(c)
	parent_id := gconv.Int(req["parent_id"])
	if parent_id == 0 {
		response.Error(c, "参数缺失【parent_id】", nil)
		return
	}
	list, err := service.CourseMenu.List(c, parent_id)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	var ids []int
	for _, menu := range list {
		ids = append(ids, menu.Id)
	}
	id := gconv.Int(req["id"])
	pageSize := gconv.Int(req["pageSize"])
	pageNo := gconv.Int(req["pageNo"])
	if pageSize == 0 || pageNo == 0 {
		response.Error(c, "参数缺失【pageSize/pageNo】", nil)
		return
	}
	var resp model.CourseListRsp
	if list == nil {
		resp.List = nil
		resp.Total = 0
	} else {
		menuList, total, err := service.CourseList.Init().MenuList(c, &model.CourseListReq{
			PageSize: pageSize,
			PageNo:   pageNo,
			Ids:      ids,
			Id:       id,
		})
		if err != nil {
			response.Error(c, err.Error(), nil)
			return
		}
		resp.List = menuList
		resp.Total = total
	}
	resp.Menu = &list
	response.Success(c, "获取成功", resp)
	return
}
func (ctl *courseListCtl) Info(c *gin.Context) {
	req := request.JsonMap(c)
	id := gconv.Int(req["id"])
	if id == 0 {
		response.Error(c, "参数缺失【id】", nil)
		return
	}
	info, err := service.CourseList.Info(c, id,
		"*")
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	share, err := service.CourseShare.Info(c, id, "id")
	if share.Id != 0 {
		info.IsCollect = 1
	} else {
		info.IsCollect = 0
	}
	if info.IsFeel == 1 {
		info.IsBuy = 1
	} else {
		buyInfo, err := service.CourseOrder.BuyInfo(c, &model.CourseBuyInfo{
			Id:        id,
			VideoType: 1,
		})
		if err != nil {
			response.Error(c, err.Error(), nil)
			return
		}
		info.IsBuy = buyInfo.IsBuy
	}
	response.Success(c, "获取成功", info)
	return
}
func (ctl *courseListCtl) LecturerMenu(c *gin.Context) {
	var req model.CourseListReq
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	list, total, err := service.CourseList.Init().MenuList(c, &req)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "获取成功", gin.H{"total": total, "list": list})
	return
}
func (ctl *courseListCtl) Search(c *gin.Context) {
	var req model.ReqList
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	list, total, err := service.CourseList.List(c, &req)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "获取成功", gin.H{"total": total, "list": list})
	return
}
